package com.sx.pipeline.ui

import android.Manifest
import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavCallback
import com.blankj.utilcode.util.PermissionUtils
import com.gyf.immersionbar.ImmersionBar
import com.lxj.xpopup.XPopup
import com.permissionx.guolindev.PermissionX
import com.sx.pipeline.app.base.CYViewBindingBaseActivity
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.custom.dialog.DialogAgreement
import com.sx.pipeline.databinding.ActLaunchBinding
import com.sx.pipeline.utils.PermissionHelper
import io.rong.callkit.util.SPUtils


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 启动页
 */
class LaunchActivity : CYViewBindingBaseActivity<ActLaunchBinding, CYBaseViewModel>() {

    override fun initBinding(inflater: LayoutInflater, container: ViewGroup?): ActLaunchBinding {
        // 兼容Android 12默认使用的SplashScreen 具体看依赖配置
        installSplashScreen()
        return ActLaunchBinding.inflate(inflater)
    }

    override fun initStatusBar() {
        ImmersionBar.with(this)
            .fitsSystemWindows(false).init()
    }

    @SuppressLint("InlinedApi")
    override fun initData() {
        fun startApp() {
            initMain()
//            PermissionX.init(this)
//                .permissions(
//                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                    Manifest.permission.READ_EXTERNAL_STORAGE,
//                    Manifest.permission.ACCESS_FINE_LOCATION,
//                    Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
//                    Manifest.permission.ACCESS_BACKGROUND_LOCATION,
//                    Manifest.permission.READ_PHONE_STATE
//                )
//                .request { _, _, _ ->
//                    initMain()
//                }
        }

        var isFirst = SPUtils.get(this, "firstStart", false) as Boolean
        if (!isFirst) {
            // 申请权限
            XPopup.Builder(this)
                .borderRadius(42f)
                .isDarkTheme(false)
                .asCustom(DialogAgreement(this, {
                    // 协议
                    mViewModel.getAgreement(it)
                }, {
                    startApp()
                },{
                    System.exit(0)
                }))
                .show()
        } else {
            startApp()
        }


    }

    private fun initMain() {
        if (TextUtils.isEmpty(mViewModel.mModel.getToken())) {
            RouteManager.Login.intentToLogin(this, navCallback)
            return
        }
        RouteManager.Main.intentToMain(this, navCallback)
    }

    private val navCallback: NavCallback = object : NavCallback() {
        override fun onArrival(postcard: Postcard?) {
            finish()
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 协议
        observe(mViewModel.agreementEvent) {
            RouteManager.Register.intentToAgreement(it!!)
        }
    }


}